package com.groupbuying.storeinternal.service.impl;

import com.groupbuying.storeinternal.connector.SysconfigFeign;
import com.groupbuying.storeinternal.constants.CommonConstants;
import com.groupbuying.storeinternal.constants.StoreOpexRuleRelPreSearchConstants;
import com.groupbuying.storeinternal.dao.StoreOpexRuleRelPreDao;
import com.groupbuying.storeinternal.domain.StoreMainInfoDO;
import com.groupbuying.storeinternal.domain.StoreOpexRuleRelPreDO;
import com.groupbuying.storeinternal.dto.StoreOperRuleDTO;
import com.groupbuying.storeinternal.service.StoreMainInfoService;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelPreService;
import com.groupbuying.storeinternal.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service
public class StoreOpexRuleRelPreServiceImpl implements StoreOpexRuleRelPreService {
	@Autowired
	private StoreOpexRuleRelPreDao storeOpexRuleRelPreDao;

	@Autowired
	private StoreMainInfoService storeMainInfoService;

	@Autowired
	private SysconfigFeign sysconfigFeign;
	
	@Override
	public StoreOpexRuleRelPreDO get(Integer preId){
		return storeOpexRuleRelPreDao.get(preId);
	}
	
	@Override
	public List<StoreOpexRuleRelPreDO> list(Map<String, Object> map){
		return storeOpexRuleRelPreDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return storeOpexRuleRelPreDao.count(map);
	}
	
	@Override
	public int save(StoreOpexRuleRelPreDO storeOpexRuleRelPre){
		return storeOpexRuleRelPreDao.save(storeOpexRuleRelPre);
	}
	
	@Override
	public int update(StoreOpexRuleRelPreDO storeOpexRuleRelPre){
		return storeOpexRuleRelPreDao.update(storeOpexRuleRelPre);
	}
	
	@Override
	public int remove(Integer preId){
		return storeOpexRuleRelPreDao.remove(preId);
	}
	
	@Override
	public int batchRemove(Integer[] preIds){
		return storeOpexRuleRelPreDao.batchRemove(preIds);
	}

	@Override
	public R getOpexRuleList(Map<String, Object> map) {
		// 解析map参数
		// 门店uuid
		Object temp = map.get("storeUuid");

		String storeUuid = temp != null ? temp.toString() : null;
		if (StringUtils.isEmpty(storeUuid)) {
			return R.paramIsNull();
		}

		Map searchMap = new HashMap();
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DELFLAG, CommonConstants.COMMON_FLAG_UNDEL);
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_STOREID, storeUuid);
		StoreMainInfoDO storeMainInfoDO = storeMainInfoService.get(storeUuid);
		List<StoreOpexRuleRelPreDO> list_opex = this.list(searchMap);
		ArrayList arrayListResult = new ArrayList();
		for (int day = 1; day <= 7; day++) {
			StoreOperRuleDTO storeOperRuleDTO = new StoreOperRuleDTO();
			storeOperRuleDTO.setStoreName(storeMainInfoDO.getStorename());
			storeOperRuleDTO.setStoreUuid(storeUuid);
			String dayDes = null;
			switch (day) {
				case 1: {
					dayDes = "星期一";
				}
				break;
				case 2: {
					dayDes = "星期二";
				}
				break;
				case 3: {
					dayDes = "星期三";
				}
				break;
				case 4: {
					dayDes = "星期四";
				}
				break;
				case 5: {
					dayDes = "星期五";
				}
				break;
				case 6: {
					dayDes = "星期六";
				}
				break;
				case 7: {
					dayDes = "星期日";
				}
				break;
			}
			storeOperRuleDTO.setDayDes(dayDes);
			storeOperRuleDTO.setDayId(day);
			storeOperRuleDTO.setOpexDes("未设置");
			storeOperRuleDTO.setTextDes("未设置");
			for (StoreOpexRuleRelPreDO storeOpexRuleRelPreDO : list_opex) {
				if (storeOpexRuleRelPreDO.getDayId().equals(String.valueOf(day))) {
					Map ruleMap = new HashMap();
					ruleMap.put("ruleId", storeOpexRuleRelPreDO.getRuleId());
					ruleMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
					Map ruleRes = sysconfigFeign.getStoreOpexRule(ruleMap);
					if (ruleRes != null) {
						//BigDecimal backCurrency = new BigDecimal(ruleRes.get("backCurrencyRatio").toString());
						//backCurrency = backCurrency.multiply(new BigDecimal(100));
						//BigDecimal discount = new BigDecimal(ruleRes.get("currencyPayDiscount").toString());
						//discount = discount.multiply(new BigDecimal(10));
						storeOperRuleDTO.setOpexDes("平台对商户的结算比例" + ruleRes.get("storeSettlement").toString() + "%");
						storeOperRuleDTO.setTextDes(storeOpexRuleRelPreDO.getRuleDes());
					}
				}
			}
			arrayListResult.add(storeOperRuleDTO);
		}

		return R.ok().put("result",arrayListResult);
	}

	@Override
	public R editOpexRule(Map<String, Object> map){
		// 解析map参数
		// 门店uuid
		Object temp = map.get("storeUuid");
		String storeUuid = temp != null ? temp.toString() : null;
		temp = map.get("dayId");
		String dayId = temp != null ? temp.toString() : null;
		temp = map.get("ruleId");
		String ruleId = temp != null ? temp.toString() : null;
		temp = map.get("ruleDes");
		String ruleDes = temp != null ? temp.toString() : null;

		if (StringUtils.isEmpty(storeUuid) || StringUtils.isEmpty(dayId) || StringUtils.isEmpty(ruleId) || StringUtils.isEmpty(ruleDes)) {
			return R.paramIsNull();
		}

		Map searchMap = new HashMap();
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_STOREID,storeUuid);
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DAYID,dayId);
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
		List<StoreOpexRuleRelPreDO> listPre = this.list(searchMap);
		if(listPre != null && listPre.size() != 0){
			//执行更新
			StoreOpexRuleRelPreDO storeOpexRuleRelPreDO = listPre.get(0);
			storeOpexRuleRelPreDO.setRuleId(ruleId);
			storeOpexRuleRelPreDO.setGmtModified(new Date());
			storeOpexRuleRelPreDO.setRuleDes(ruleDes);
			this.update(storeOpexRuleRelPreDO);
			return R.ok();
		}

		//执行插入
		StoreOpexRuleRelPreDO storeOpexRuleRelPreDO = new StoreOpexRuleRelPreDO();
		storeOpexRuleRelPreDO.setStoreId(storeUuid);
		storeOpexRuleRelPreDO.setDayId(dayId);
		storeOpexRuleRelPreDO.setDelFlag(CommonConstants.COMMON_FLAG_UNDEL);
		storeOpexRuleRelPreDO.setGmtModified(new Date());
		storeOpexRuleRelPreDO.setRuleDes(ruleDes);
		storeOpexRuleRelPreDO.setRuleId(ruleId);
		this.save(storeOpexRuleRelPreDO);
		return R.ok();
	}
}
